package hips.images.integer.rgb;

import hips.Image;
import hips.images.integer.ImageInt;
import ij.ImagePlus;

public class ImageRGB extends ImageInt<PixelValueRGB> {
	public int[][] pixels;

	public ImageRGB(ImagePlus impl) {
		height = impl.getHeight();
		width = impl.getWidth();
		size = height * width;
		slices = impl.getStackSize();
		channels = 3;
		pixels = new int[slices][];
		for (int i = 0; i < slices; i++) {
			pixels[i] = (int[]) impl.getStack().getProcessor(i + 1).getPixels();
		}
		initialize(impl);
	}

	public ImageRGB(ImageRGB img) {
		height = img.getHeight();
		width = img.getWidth();
		size = height * width;
		slices = img.getSlices();
		channels = 1;
		ImagePlus impl = img.getImagePlus().createImagePlus();
		impl.setTitle("Created from:  " + img.getTitle());
		ij.ImageStack stack = new ij.ImageStack(width, height);
		for (int i = 0; i < img.getSlices(); i++) {
			stack.addSlice("" + i, img.getImagePlus().getStack().getProcessor(i + 1).createProcessor(width, height));
		}
		impl.setStack(stack);
		pixels = new int[slices][];
		for (int i = 0; i < slices; i++) {
			pixels[i] = (int[]) impl.getStack().getProcessor(i + 1).getPixels();
		}
		initialize(impl);
	}

	public void putPixelValue(int index, PixelValueRGB pixel) {
		for (int i = 0; i < slices; i++) {
			pixels[i][index] = pixel.getValue(i);
		}
	}

	public PixelValueRGB newPixelValue(Number value) {
		PixelValueRGB p = new PixelValueRGB(slices);
		for (int s = 0; s < slices; s++) {
			for (int c = 0; c < channels; c++) {
				p.setValue(value, s, c);
			}
		}
		return p;
	}

	public PixelValueRGB newPixelValue() {
		PixelValueRGB p = new PixelValueRGB(slices);
		for (int s = 0; s < slices; s++) {
			for (int c = 0; c < channels; c++) {
				p.setValue(0, s, c);
			}
		}
		return p;
	}

	public PixelValueRGB getPixelValue(int index) {
		PixelValueRGB pixel = new PixelValueRGB(slices);
		for (int i = 0; i < slices; i++) {
			pixel.setValue(pixels[i][index], i);
		}
		return pixel;
	}
	
	public PixelValueRGB getPixelValue(PixelValueRGB pvalue, int index) {
		for (int i = 0; i < slices; i++) {
			pvalue.setValue(pixels[i][index], i);
		}
		return pvalue;
	}

	public Image<PixelValueRGB> newImage() {
		return new ImageRGB(this);
	}
}